<?php

namespace app\api\controller;


use addons\epay\library\Service;
use app\api\model\Auction as AuctionModel;
use app\api\model\AuctionBidLog;
use app\api\model\AuctionOrder as AuctionOrderModel;
use app\api\model\UserAddress as UserAddressModel;
use app\common\controller\Api;
use fast\Random;

/**
 * 拍卖订单接口
 */
class Auctionorder extends Api
{

    protected $noNeedRight = ['*'];

    //尾款支付
    public function finalpay()
    {
        $order_no = input('final_order_no','');
        $pay_type = input('pay_type','wechat');
        $method = input('method','miniapp');
        $order = AuctionOrderModel::get(['final_order_no'=>$order_no]);

        if (!$order){
            $this->error('订单不存在');
        }

        if ($order->final_pay_status==1)$this->error('已支付');

        $user = \app\common\model\User::get($this->auth->id);


        $request = request();
        $notifyurl =  $request->root(true)."/api/callback/finalnotify";
        $title = $order['auction_name'].'尾款';
        try {
            $pay = Service::submitOrder($order['final_payment'],$order['final_order_no'],$pay_type,$title,$notifyurl,'',$method,$user->wechat_open_id);
        }catch (\Exception $e) {
            // 回滚事务
            $this->error($e->getMessage());
        }

        $this->success('',$pay);

    }

    //尾款线下支付
    public function finalofflinepay()
    {
        $order_no = input('final_order_no','');

        $model = new AuctionOrderModel();

        $res = $model->finalnotify($order_no);
        $this->success('提交成功',$res);
    }

    //定金支付
    public function pay()
    {
        $order_no = input('order_no','');
        $pay_type = input('pay_type','wechat');
        $method = input('method','miniapp');
        $order = AuctionOrderModel::get(['order_no'=>$order_no]);

        if (!$order){
            $this->error('订单不存在');
        }

        if ($order->pay_status==1)$this->error('已支付');

        $user = \app\common\model\User::get($this->auth->id);


        $request = request();
        $notifyurl =  $request->root(true)."/api/callback/notify";
        $title = $order['auction_name'].'定金';
        try {
            $pay = Service::submitOrder($order['price'],$order['order_no'],$pay_type,$title,$notifyurl,'',$method,$user->wechat_open_id);
        }catch (\Exception $e) {
            // 回滚事务
            $this->error($e->getMessage());
        }

        $this->success('',$pay);

    }

    //定金线下支付
    public function offlinepay()
    {
        $order_no = input('order_no','');

        $model = new AuctionOrderModel();

        $res = $model->notify($order_no);
        $this->success('提交成功',$res);
    }

    //我的订单
    public function lists()
    {

        $order_status = input('order_status','all');
        $auction_model = new AuctionOrderModel();

        $list = $auction_model->lists($this->auth->id,$order_status);
        $this->success('',$list);
    }

    //我的拍卖
    public function auctionlists()
    {
        try {
            $param = input();
            $auction_model = new AuctionModel();
            $param['user_id'] = $this->auth->id;
            $list = $auction_model->mylists($param);
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$list);

    }

}